<?php

namespace addons\shopro\controller;

use app\admin\model\shopro\Category as CategoryModel;
use think\Db;

class Category extends Common
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function index()
    {
        $id = $this->request->param('id', 0);
        $category = CategoryModel::where('parent_id', 0)->normal()->order('weigh', 'desc')->order('id', 'desc');
        if ($id) {
            // 指定 id 分类，否则获取 权重最高的一级分类
            $category = $category->where('id', $id);
        }
        $category = $category->find();
        if (!$category) {
            $this->error(__('No Results were found'));
        }

        $childrenString = $category->getChildrenString($category);
        $categories = CategoryModel::where('id', $category->id)->normal()->with([$childrenString])->find();
        
        
        //返回开始和结束时间
        
        //判断是否为店铺分类
        if($category['is_shop'] == 1){
            $allshopcates =  Db::name('shopro_category')->where(['parent_id'=>0,'is_shop'=>1])->select();
            $categories['allshop'] = $allshopcates;
        }
        if($categories['shop_opentime']){
            if(str_replace(':','.',$categories['shop_opentime'])<12){
                $categories['shop_opentime'] = '上午 '.$categories['shop_opentime'];
            }else{
                $categories['shop_opentime'] = '下午 '.$categories['shop_opentime'];
            }
        }
        if($categories['shop_stoptime']){
            if(str_replace(':','.',$categories['shop_stoptime'])<12){
                $categories['shop_stoptime'] = '凌晨 '.$categories['shop_stoptime'];
            }else{
                 
                $categories['shop_stoptime'] = '下午 '.$categories['shop_stoptime'];
            }
        }
        foreach($categories['children'] as $k=>&$v){
            if($v['shop_opentime']){
                if(str_replace(':','.',$v['shop_opentime'])<12){
                    $v['shop_opentime'] = '上午 '.$v['shop_opentime'];
                }else{
                    $v['shop_opentime'] = '下午 '.$v['shop_opentime'];
                }
            }  
            if($v['shop_stoptime']){
                if(str_replace(':','.',$v['shop_stoptime'])<12){
                    $v['shop_stoptime'] = '凌晨 '.$v['shop_stoptime'];
                }else{
                    $v['shop_stoptime'] = '下午 '.$v['shop_stoptime'];
                } 
            }
        }
        $this->success('商城分类', $categories);
    }
}
